package com.wallet.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.wallet.entity.Role;
import com.wallet.entity.RoleMenu;
import com.wallet.entity.Route;
import com.wallet.entity.form.RolesForm;
import com.wallet.entity.form.RoutesForm;
import com.wallet.entity.result.JsonResult;
import com.wallet.entity.result.ResultCode;
import com.wallet.service.Impl.RoleMenuServiceImpl;
import com.wallet.service.Impl.RoleServiceImpl;
import com.wallet.service.Impl.RouteServiceImpl;
import com.wallet.util.HttpClientUtil;
import com.wallet.util.RouteTreeUtil;

@RestController
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private RoleServiceImpl roleServiceImpl;
	
	@Autowired
	private RouteServiceImpl routeServiceImpl;
	
	@Autowired
	private RoleMenuServiceImpl roleMenuServiceImpl;
	
	public List<RoutesForm> getRoutes(Integer roleId){
		List<RoutesForm> list=new ArrayList<RoutesForm>();
		list=routeServiceImpl.selectRouteByRoleId(roleId);
		List<RoutesForm> listresult=RouteTreeUtil.buildTree(list, 0);
		return listresult;
	}
	
	@RequestMapping("/listTree")
	public JsonResult getAll() {
		JsonResult result=new JsonResult();
		result.setCode(ResultCode.SUCCESS.getVal());
		List<Role> list=roleServiceImpl.selectAll();
		List<RolesForm> data=new ArrayList<RolesForm>();
		for (Role role : list) {
			RolesForm rolesForm=new RolesForm();
			rolesForm.setDescription(role.getDescription());
			rolesForm.setId(role.getId());
			rolesForm.setKey(role.getKey());
			rolesForm.setName(role.getName());
			rolesForm.setRoutes(getRoutes(role.getId()));
			data.add(rolesForm);
		}
		result.setData(data);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/init")
	public String init() {
		String result=HttpClientUtil.get("http://localhost:9527/dev-api/vue-element-admin/roles");
		Map<String, Object> map=(Map<String,Object>)JSON.parse(result);
		String data=map.get("data").toString();
		List<RolesForm> list=JSON.parseArray(data, RolesForm.class);
		for (RolesForm rolesForm : list) {
			//保存权限数据
			Role role=new Role();
			role.setKey(rolesForm.getKey());
			role.setDescription(rolesForm.getDescription());
			role.setName(rolesForm.getName());
			roleServiceImpl.save(role);
			
			//权限路由处理
			List<RoutesForm> routes=rolesForm.getRoutes();
			for (RoutesForm routeForm : routes) {
				saveRoleMenu(routeForm, role.getId());
			}
		}
		return list.toString();
	}
	
	private void saveRoleMenu(RoutesForm routesForm,Integer roleId) {
		Route route=new Route();
		route.setComponent(routesForm.getComponent());
		route.setName(routesForm.getName());
		route.setPath(routesForm.getPath());
		route.setRedirect(routesForm.getRedirect());
		route=routeServiceImpl.selectRouteByAll(route);
		RoleMenu roleMenu=new RoleMenu();
		roleMenu.setRoleId(roleId);
		roleMenu.setRouteId(route.getId());
		roleMenuServiceImpl.save(roleMenu);
		List<RoutesForm> childrenRoute=routesForm.getChildren();
		if(childrenRoute!=null && childrenRoute.size()>0) {
			for (RoutesForm routesFormchildren : childrenRoute) {
				saveRoleMenu(routesFormchildren, roleId);
			}
		}
	}

}
